var opttype = "save";// save=新增,edit=修改，delete=删除,show=查看,reload=刷新
var url = "save";
var indexname = "";
$(document).ready(function() {
	//隔行变色
	$("#dg").datagrid({
	rowStyler:function(index,row){    
        if (index%2==1){    
            return 'background-color:#C8E1FD;';    
        }    
    }    
    });
	// 查询
	$("#querybtn").click(function() {
		onOpenQueryDialog();
	});
	// 重置
	$("#resetbtn").click(function() {
		onResetHandler();
	});
	// 新增
	$("#newbtn").click(function() {
		onNewHandler();
	});
	// 修改
	$("#editbtn").click(function() {
		onEditHandler();
	});
	// 删除
	$("#deletebtn").click(function() {
		onDeleteHandler();
	});
	// 刷新
	$("#reloadbtn").click(function() {
		$("#dg").datagrid("load",{});
		$("#dg").datagrid("unselectAll");
	});
	// 新增，修改 提交
	$("#dlg-savebtn").click(function() {
		onSubmit();
	});
	// 关闭弹出窗口
	$("#dlg-closebtn").click(function() {
		$("#dlg").dialog("close");
	});

	// 提交查询
	$("#dlg-query-savebtn").click(function() {
		onQueryHandler();
	});

	// 关闭弹出的查询窗口
	$("#dlg-query-closebtn").click(function() {
		$("#dlg-query").dialog("close");
	});

	// 表格双击
//	$("#dg").datagrid({
//		"onDblClickRow" : function(index, row) {
//			iconCls: "icon-search", onShowHandler(row);
//		}
//	});
});

// 打开查询窗口
function onOpenQueryDialog() {
	$("#dataform-query").form("clear");

	$("#dlg-query").dialog({
		iconCls : "icon-search",
		title : "查询" + indexname
	});
	$("#dlg-query").dialog("open");
}
function onQueryHandler() {
	var param = {};
	try {
		if (subQueryHandler && typeof (subQueryHandler) == "function") {
			param = subQueryHandler();
		}
	} catch (e) {

	}

	if (param == null) {
		param = {};
	}

	var param = serializeObject($("#dataform-query").form(), param);
	$("#dg").datagrid("load", param);
	$("#dlg-query").dialog("close");
}

// easyui form 序列化为 object
function serializeObject(form, param) {
	var o = param;
	$.each(form.serializeArray(), function(index) {
		if (o[this["name"]]) {
			if (this["value"] != null && this["value"] != "") {
				o[this["name"]] = o[this["name"]] + "," + this["value"];
			}
		} else {
			if (this["value"] != null && this["value"] != "") {
				o[this["name"]] = this["value"];
			}
		}
	})
	return o;
}

// 重置
function onResetHandler() {
	try {
		if (subResetHandler && typeof (subResetHandler) == "function") {
			subResetHandler();
		}
	} catch (e) {

	}
}

// 新增
function onNewHandler() {
	$("#dlg-savebtn").linkbutton("enable");
	$("#dataform").form("clear");
	$("#dlg").dialog({
		iconCls : "icon-add",
		title : "新增" + indexname
	});
	$("#dlg").dialog("open");
	$("#dlg").window("center");
	opttype = "save";
	$("#iid").val(0);
	$("#dlg-savebtn").linkbutton({
		"text" : "保存"
	});
	$("#dlg-savebtn").show();
	try {
		if (subNewHandler && typeof (subNewHandler) == "function") {
			subNewHandler();
		}
	} catch (e) {

	}
}
// 修改
function onEditHandler() {
	$("#dlg-savebtn").linkbutton("enable");
	var rows = $("#dg").datagrid("getSelections");
	if (rows == null || rows.length == 0) {
		$.messager.alert("提示", "请选择后，再操作！");
		return;
	}
	if (rows.length != 1) {
		$.messager.alert("提示", "只能选择一条数据进行操作！");
		return;
	}
	var row = rows[0];
	onOldEditHandler(row);
}
function onOldEditHandler(row) {
	
	if (row == null) {
		$.messager.alert("提示", "请选择后，再操作！");
		return;
	}
	// 清空表单
	$("#dataform").form("clear");
	$("#dlg").dialog({
		iconCls : "icon-edit",
		title : "修改" + indexname
	});
	// $("#dlg").dialog("setTitle", "编辑");
	$("#dlg").dialog("open");
	$("#dlg").window("center");
	opttype = "edit";
	$("#dlg-savebtn").linkbutton({
		"text" : "修改"
	});
	try {
		if (subEditHandler && typeof (subEditHandler) == "function") {
			subEditHandler(row);
		}
	} catch (e) {

	}
	$("#dataform").form("load", row);
	$("#dlg-savebtn").show();
}

// 查看
function onShowHandler(row) {
	if (row == null) {
		$.messager.alert("提示", "请选择后，再操作！");
		return;
	}
	// 清空表单
	$("#dataform").form("clear");
	$("#dataform").form("load", row);
	$("#dlg").dialog({
		iconCls : "icon-search",
		title : "查看" + indexname
	});

	$("#dlg").dialog("open");
	$("#dlg").window("center");
	opttype = "show";
	$("#dlg-savebtn").linkbutton({
		"text" : "查看"
	});
	$("#dlg-savebtn").hide();
	try {
		if (subShowHandler && typeof (subShowHandler) == "function") {
			subShowHandler(row);
		}
	} catch (e) {

	}
}

// 删除
function onDeleteHandler() {
	var rows = $("#dg").datagrid("getSelections");
	if (rows == null || rows.length == 0) {
		$.messager.alert("提示", "请选择后，再操作！");
		return;
	}
	var ids = Common.getIds(rows, "iid");
	onOldDeleteHandler(ids);
}
function onOldDeleteHandler(ids) {

	if (ids == "") {
		$.messager.alert("提示", "请选择后，再操作！");
		return;
	}
    $.messager.defaults = {ok: "确定", cancel: "否"};
    $.messager.confirm("提示", "是否删除选择项?", function (issure) {
		if (issure) {
			submitDeleteHandler(ids);
		}
    }).window({width: 300, height: 200});
    ;
}

// 提交删除操作
function submitDeleteHandler(ids) {
	Common.closeProgress();
	Common.ajax("removeByIds", {
		"ids" : ids
	}, function(data) {
		Common.showAlert("", data.msg, "info")
		onClearSelected();
		$("#dg").datagrid("reload");
	}, null);
}
// 根据行号来查找行数据
function getRowById(rowIndex) {
	var data = $("#dg").datagrid("getData").rows[rowIndex];
	return data;
}
// 点击修改
function onItemEdit(index) {
	onClearSelected();
	var rowData = getRowById(index);
	onOldEditHandler(rowData);
}
// 点击删除
function onItemDelete(index) {
	onClearSelected();
	var rowData = getRowById(index);
	onOldDeleteHandler(rowData.iid)
}
// 点击查看
function onItemShow(index) {
	onClearSelected();
	var rowData = getRowById(index);
	onShowHandler(rowData);
}

// 表格取消选中
function onClearSelected() {
	$("#dg").datagrid("unselectAll");
	$("#dg").datagrid("clearSelections");
}

function onItemOptEdit(value, row, index) {
	return "<a class='easyui-linkbutton dgitemedit' style='width:25px;height:16px;' iconCls='icon-edit' onclick='onItemEdit("
			+ index + ")' title='编辑' plain='true'></a> ";
}
function onItemOptDelete(value, row, index) {
	return "<a class='easyui-linkbutton dgitemremove' style='width:25px;height:16px;' iconCls='icon-remove' onclick='onItemDelete("
			+ index + ")' title='删除' plain='true'></a> ";
}
function onItemOptShow(value, row, index) {
	return "<a class='easyui-linkbutton dgitemshow' style='width:25px;height:16px;' iconCls='icon-search' onclick='onItemShow("
			+ index + ")' title='查看' plain='true'></a> ";
}



function onDGLoadSuccess(data) {
	$(".dgitemedit").linkbutton({
		text : "",
		plain : true,
		iconCls : "icon-edit"
	});
	$(".dgitemremove").linkbutton({
		text : "",
		plain : true,
		iconCls : "icon-remove"
	});
	$(".dgitemshow").linkbutton({
		text : "",
		plain : true,
		iconCls : "icon-search"
	});
	$(".dgitemresetPwd").linkbutton({
		text : "",
		plain : true,
		iconCls : "icon-reload"
	});
	$(".dgMenuChoose").linkbutton({
		text : "",
		plain : true,
		iconCls : "icon-redo"
	});
	try {
		if (subDGLoadSuccess && typeof (subDGLoadSuccess) == "function") {
			subDGLoadSuccess(data);
		}
	} catch (e) {

	}

}

// 提交
function onSubmit() {
	$("#dlg-savebtn").linkbutton("disable");
	if (opttype == "save") {
		url = "save";
	} else {
		url = "edit";
	}

	try {
		if (subSubmitHandler && typeof (subSubmitHandler) == "function") {
			subSubmitHandler();
		}
	} catch (e) {

	}
	//Common.showProgress("正在请求中...");
	$("#dataform").form("submit", {
		url : url,
		onSubmit : function() {
			var bool = $("#dataform").form("validate");
			if (bool == false) {
				$("#dlg-savebtn").linkbutton("enable");
				Common.closeProgress();
			}
			return bool;
		},
		error : function(XMLHttpRequest, textStatus, errorThrow) {
			$("#dlg-savebtn").linkbutton("enable");
			Common.showAlert("请求错误", XMLHttpRequest.responseText, "error");
			Common.closeProgress();
		},
		success : function(data, textStatus) {
			Common.closeProgress();
			$("#dlg-savebtn").linkbutton("enable");
			var answerJson = eval('(' + data + ')');
			Common.showAlert("提示", answerJson.msg, "info");
			if (answerJson.success) {
				$("#dataform").form("clear");
				$("#dlg").dialog("close");
				onClearSelected();
				$("#dg").datagrid("reload");
			}
			
		}
	});
}

// 重新加载页面
function reloadClass() {
	$("#dl").datalist("reload");
	$("#dl").datalist("unselectAll");
	$("#dg-push").datagrid({
		queryParams : {
			cname : ""
		}
	});
};